package com.yxb.cms.architect.config;

import com.google.common.cache.CacheBuilder;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.guava.GuavaCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.concurrent.TimeUnit;


/**
 * 基于Guava的本地化缓存
 * @author YXB
 */
@Configuration
@EnableCaching
public class GuavaCacheConfig {


    /**
     * GuavaCacheManager 缓存时长1天，可自定义
     * @return
     */
    @Primary
    @Bean
    public CacheManager guavaCacheManagerDays() {
        GuavaCacheManager cacheManager = new GuavaCacheManager();
        cacheManager.setCacheBuilder(CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.DAYS));
        return cacheManager;
    }

    /**
     * GuavaCacheManager 缓存时长10分钟可自定义
     * @return
     */
    @Bean(name = "cacheManagerMinutes")
    public CacheManager guavaCacheManagerMinutes() {
        GuavaCacheManager cacheManager = new GuavaCacheManager();
        cacheManager.setCacheBuilder(CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES));
        return cacheManager;
    }

    /**
     * GuavaCacheManager 缓存时长10秒钟可自定义
     * @return
     */
    @Bean(name = "cacheManagerSecounds")
    public CacheManager guavaCacheManagerSecounds() {
        GuavaCacheManager cacheManager = new GuavaCacheManager();
        cacheManager.setCacheBuilder(CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS));
        return cacheManager;
    }

}
